package com.zhi.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import com.zhi.dao.UserRepository;
import com.zhi.entity.User;
import com.zhi.service.UserService;

@Service
public class UserServiceImpl implements UserService{
	
	@Autowired
	private UserRepository userRepository;


	@Override
	public List<User> getEntityList() {
		return userRepository.findAll();
	}
	
	@Override
	public List<Object[]> getEntityListWithCodeName() {
		return userRepository.findAllWithTypeName();
	}
	

	@Override
	public Page<User> getEntityListWithCodeName(Pageable pageable) {
		return userRepository.findAllWithTypeName(pageable);
	}

	@Override
	public User getOne(Integer id) {
		return userRepository.getOne(id);
	}
	
	@Override
	public List<User> findAll(User user) {
		Example<User> example =Example.of(user);
		return userRepository.findAll(example);
	}

	@Override
	public void save(User user) {
		userRepository.save(user);
	}

	@Override
	public void edit(User user) {
		userRepository.save(user);
	}

	@Override
	public void delete(Integer id) {
		userRepository.deleteById(id);
	}

	@Override
	public void deleteByUsernameAndUserType(String username,String userType){
		userRepository.deleteByUsernameAndUserType(username,userType);
	}

	@Override
	public Integer count(User user) {
		Example<User> example =Example.of(user);
		return (int)userRepository.count(example);
	}

	@Override
	public User finByName(String username) {
		return userRepository.findByName(username);
	}

}
